<template>
  <el-card shadow="hover">
    <el-form
      label-width="120px"
      inline
    >
      <el-form-item label="一级分类">
        <el-select placeholder="请选择" v-model="category1Id" :disabled="disabled" size="small">
          <el-option
            :label="c1.name"
            :value="c1.id"
            v-for="c1 in category1List"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select placeholder="请选择" v-model="category2Id" :disabled="disabled" size="small">
          <el-option
            :label="c2.name"
            :value="c2.id"
            v-for="c2 in category2List"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="category3Id" :disabled="disabled" size="small">
          <el-option
            :label="c3.name"
            :value="c3.id"
            v-for="c3 in category3List"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";

export default {
  name: "CategorySelector",
  data() {
    return {
      // CategorySelectorForm: {},
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,
    };
  },
  props: {
    disabled: {
      type: Boolean /* 类型 */,
      default: true /* 外界没有传值 则默认值为100  */,
      required: false /*必填  */,
    },
    // msg: String /* 没有默认值 可以这样写  只是可读属性 不能直接修改会报错 你可以把值转接到data属性中 */,
  },
  computed: {
    ...mapState("category", [
      "category1List",
      "category2List",
      "category3List",
    ]),
  },
  methods: {
    ...mapActions("category", [
      "getCategory1List",
      "getCategory2List",
      "getCategory3List",
    ]),
    ...mapMutations("category", ["UPDATE_CATEGORY3ID"]),
  },
  async mounted() {
    this.getCategory1List();
  },
  watch: {
    category1Id(category1Id) {
      if(!category1Id) return
      this.category2Id = undefined;
      this.category3Id = undefined;
      this.getCategory2List(category1Id);
    },
    category2Id(category2Id, oldVal) {
      if (!category2Id) return;
      this.category3Id = undefined;
      this.getCategory3List(category2Id);
    },
    category3Id: {
      handler(category3Id) {
        this.UPDATE_CATEGORY3ID(category3Id);
      },
      // immediate: true,
    },
  },
};
</script>

<style scoped>
::v-deep .el-form-item {
  margin-top: 10px;
  margin-bottom: 10px;
}
</style>
